import { Component } from './Component';
import { EntityNode } from './EntityNode';
import { NodeSystem } from './NodeSystem';

/**
 * 注册全局已注册节点实例映射，便于通过entity获取节点实例
 */
export const registryEntityNodeByEntity: Map<any, EntityNode> = new Map();

/**
 * 注册全局已注册节点实例映射，便于通过uuid获取节点实例
 */
export const registryEntityNodeByUUID: Map<string, EntityNode> = new Map();

/**
 * 注册全局组件实例映射
 */
export const registryComponent: Map<string, typeof Component> = new Map();

/**
 * 注册全局系统实例映射
 */
export const registryNodeSystem: Map<string, NodeSystem> = new Map();

/**
 * 根据entity或uuid从全局注册表中获取一个已注册的节点实例。
 * - 优先级：entity > uuid
 *
 * @param entityOrUUID - 要获取的节点的实体或uuid。
 * @returns 返回对应的节点实例，如果未找到则返回 undefined。
 */
export function find<U = any>(entityOrUUID: U): EntityNode<U> | undefined {
  return (
    registryEntityNodeByEntity.get(entityOrUUID) ||
    registryEntityNodeByUUID.get(entityOrUUID as string)
  );
}
